package com.vlyman.demo.service.impl;

import com.vlyman.demo.mapper.UserMapper;
import com.vlyman.demo.model.entity.User;
import com.vlyman.demo.rtn.DemoRtn;
import com.vlyman.demo.service.UserService;
import com.vlyman.rtn.RtnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 用户业务实现类
 * @author chenjs
 * @date 2018/9/19
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 方法描述：
     *
     * @return RtnResult
     * @Description 获取全部用户
     * @author chenjs
     * @date 2018/9/19
     */
    @Override
    public RtnResult getAllUser() {
        List<User> userList = userMapper.selectList(null);
        return RtnResult.success().setData(userList);
    }

    /**
     * 方法描述：
     * 保存用户
     *
     * @param user
     * @return RtnResult
     * @author chenjs
     * @date 2018/10/9 23:10
     */
    @Override
    public RtnResult saveUser(User user) {
        userMapper.insert(user);
        if (user.getAge() == 1) {
            throw new RuntimeException("测试事务回滚");
        }
        return RtnResult.success();
    }

    /**
     * 方法描述：
     * 获取用户
     *
     * @param userId
     * @return RtnResult
     * @author chenjs
     * @date 2018/10/10 11:57
     */
    @Override
    public RtnResult getUserById(int userId) {
        if (userId == 1) {
            return RtnResult.setRtn(DemoRtn.DEMO_NO_AUTH);
        }
        User user = userMapper.selectById(userId);
        if (user == null) {
            return RtnResult.noData();
        }
        return RtnResult.success().setData(user);
    }
}
